import 'package:dio/dio.dart';
import 'dart:math';
import 'TreeHoleInfo.dart';
class TreeHoleList{
  //获取所有树洞
   static List<Map> articleList;
   static getAllArticles() async {
    var response1=await Dio().post(
      'http://121.196.101.7:8080/treehole/article/allArticles',
    );
    var response2=new Map<String, dynamic>.from(response1.data);
    var rng = new Random();
    int count = 0;
    articleList=[];//每次调用让list为空，使用dispose？
    List _list1 = response2["data"];//接口获得数组
    List _tempList=[];//暂存随机序列
    while (count < response2["data"].length) {
      int index = rng.nextInt(response2["data"].length);
      if (!_tempList.contains(index)) {
        _tempList.add(index);
        count++;
        articleList.add(_list1[index]);
      }
    }
    return articleList;
  }
  //审核
   static verifyArticles(String token) async{
    await Dio().post(
         'http://121.196.101.7:8080/treehole/article/verifyAllArticle',
         options: Options(headers: {
           'Token': token})
     );
   }
  //获取我的树洞
   static List myArticles;
   static getMyArticles(String token) async{
     var response1=await Dio().post(
         'http://121.196.101.7:8080/treehole/article/myArticles',
         options: Options(headers: {
           'Token': token})
     );
     var response2=new Map<String, dynamic>.from(response1.data);
     myArticles=response2["data"];
     return myArticles;
   }
   //获取树洞内容
   static Future getArticle(int id) async {
       return TreeHoleInfo.fromJson(
           (await Dio().post(
             'http://121.196.101.7:8080/treehole/article/article',
             queryParameters: {'id': id},
           )).data['data']
       );
     }
     //创建树洞
   static addArticle(String token,String title,String content) async {
     var response1=await Dio().post(
       'http://121.196.101.7:8080/treehole/article/addArticle',
       queryParameters: {'title':title,'content':content,'emotion':'......'},
       options: Options(headers: {'Token': token,
       }),
     );
     var response2=new Map<String, dynamic>.from(response1.data);
     verifyArticles(token);
     if (response2['data']=='true'){return '创建成功';}
     else if(response2['data']=='false'){return '创建失败';}
   }
   //删除树洞
   static deleteArticle(String token,int id) async {
     var response1=await Dio().post(
       'http://121.196.101.7:8080/treehole/article/deleteArticle',
       queryParameters: {'id':id},
       options: Options(headers: {'Token': token,
       }),
     );
     var response2=new Map<String, dynamic>.from(response1.data);
     if (response2['data']=='true'){return '删除成功';}
     else if(response2['data']=='false'){return '删除失败';}
   }
   //获取所有评论
   static List commentList;
   static getArticleComments(int id) async {
     var response1=await Dio().post(
       'http://121.196.101.7:8080/treehole/comment/articleComments',
       queryParameters: {'articleId': id},
     );
     var response2=new Map<String, dynamic>.from(response1.data);
     commentList=[];//每次调用让list为空，使用dispose？
     commentList = response2["data"];//接口获得数组
     return commentList;
   }
   //添加评论
   static addComment(String token,int articleId,String content,{int commentId}) async {
     var response1=await Dio().post(
       'http://121.196.101.7:8080/treehole/comment/addComment',
       queryParameters: {'articleId':articleId,'content':content,'commentId':commentId},
       options: Options(headers: {'Token': token,
       }),
     );
     print(response1.data);
     var response2=new Map<String, dynamic>.from(response1.data);
     if (response2['data']=='true'){return '回复成功';}
     else if(response2['data']=='false'){return '回复失败';}
   }
   //删除评论
   static deleteComment(String token,int id) async {
     var response1=await Dio().post(
       'http://121.196.101.7:8080/treehole/comment/deleteComment',
       queryParameters: {'commentId':id},
       options: Options(headers: {'Token': token,
       }),
     );
     var response2=new Map<String, dynamic>.from(response1.data);
     print(response1.data);
     if (response2['data']=='true'){return '删除成功';}
     else if(response2['data']=='false'){return '删除失败';}
     else if(response2['message']=='您不能删除此评论'){return '您不能删除此评论';}
   }
}